package P1000;

import java.util.*;

/**
 * @author: zhouyang
 * @descrpition:
 * @time: 2019-10-18 10:44
 */
public class P1056_排座椅 {

    static class Node implements Comparable<Node>{
        int val;//当前是什么位置
        int count;//出现了多少次
        @Override
        public int compareTo(Node o) {
            if(this.count == o.count) return this.val-o.val;
            return this.count-o.count;
        }
        public Node(int val,int count){
            this.val = val;
            this.count = count;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int M = scanner.nextInt();//M行
        int N = scanner.nextInt();//N列
        int K = scanner.nextInt();//画的横线
        int L = scanner.nextInt();//画的纵线
        int D = scanner.nextInt();//交头接耳的学生对数
        Map<Integer,Integer> map = new HashMap<>();
        while(D-- > 0){
            int now = help(scanner.nextInt(),scanner.nextInt(),scanner.nextInt(),scanner.nextInt());
            map.put(now,map.getOrDefault(now,0)+1);
        }
        scanner.close();
        PriorityQueue<Node> row = new PriorityQueue<>(K);
        PriorityQueue<Node> col = new PriorityQueue<>(L);
        for(Map.Entry<Integer,Integer> entry:map.entrySet()){
            if(entry.getKey() > 2000){//纵
                if(col.size() < L) col.offer(new Node(entry.getKey()-2000,entry.getValue()));//不够
                else{
                    if(entry.getValue() == col.peek().count){
                        if(entry.getKey()-2000 < col.peek().val){
                            col.poll();
                            col.offer(new Node(entry.getKey()-2000,entry.getValue()));
                        }
                    }else if(entry.getValue() > col.peek().count){
                        col.poll();
                        col.offer(new Node(entry.getKey()-2000,entry.getValue()));
                    }
                }
            }else{//横
                if(row.size() < K){
                    row.offer(new Node(entry.getKey(),entry.getValue()));
                }else{
                    if(entry.getValue() == row.peek().count){
                        if(entry.getKey() < row.peek().val){
                            row.poll();
                            row.offer(new Node(entry.getKey(),entry.getValue()));
                        }
                    }else if(entry.getValue() > row.peek().count){
                        row.poll();
                        row.offer(new Node(entry.getKey(),entry.getValue()));
                    }
                }
            }
        }
        int index = 0;
        if (K > 0) {
            int[] r = new int[K];
            while(!row.isEmpty()){
                r[index++] = row.poll().val;
            }
            Arrays.sort(r);
            System.out.printf("%d",r[0]);
            for(int i=1;i<K;++i){
                System.out.printf(" %d",r[i]);
            }
            System.out.println();
        }
        if(L > 0){
            index = 0;
            int[] c = new int[L];
            while(!col.isEmpty()){
                c[index++] = col.poll().val;
            }
            Arrays.sort(c);
            System.out.printf("%d",c[0]);
            for(int i=1;i<L;++i){
                System.out.printf(" %d",c[i]);
            }
        }
    }

    private static int help(int x,int y,int z,int k){
        if(x == z){
            return 2000+Math.min(y,k);
        }else{
            return Math.min(x,z);
        }
    }
}
